//
//  InformationPlatform.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/19.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "InformationPlatform.h"
#import "MyQRCodeViewController.h"
#import "CameraActionSheet.h"
#import "VPImageCropperViewController.h"
@interface InformationPlatform ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,CameraActionSheetDelegate> {
    UITableView *_tableView;
    NSMutableArray *_dataSource;
}

@end

@implementation InformationPlatform

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"平台信息";
    self.view.frame = [UIScreen mainScreen].bounds;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"..." style:UIBarButtonItemStyleDone target:self action:@selector(releaseBtnAction:)];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    [self.view addSubview:_tableView];
    
    _dataSource = [NSMutableArray array];
    NSArray *title = @[@"平台名称",@"云平台号",@"绑定邮箱",@"平台性质",@"地址",@"平台二维码"];
    NSArray *text = @[@"云平台",@"123321",@"123456@qq.com",@"企业平台",@"广州",@""];
    for (int i = 0; i<title.count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:title[i] forKey:@"title"];
        [dic setValue:text[i] forKey:@"text"];
        [_dataSource addObject:dic];
    }
    

}
#pragma mark --编辑按钮点击事件--
-(void)releaseBtnAction:(UIBarButtonItem *)sender {
    
}
#pragma mark -
#pragma mark --tableViewDelegate--
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
        {
            return 1;
        }
        case 1:
        {
            return _dataSource.count;
        }
            
            
        default:
            break;
    }
    return _dataSource.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"图标";
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon"]];
        image.frame = CGRectMake(CGRectGetWidth(cell.frame) - 60, 10, 40, 40);
        [cell.contentView addSubview:image];
    }else {
        cell.textLabel.text = _dataSource[indexPath.row][@"title"];
        cell.detailTextLabel.text = _dataSource[indexPath.row][@"text"];
        if (indexPath.row == _dataSource.count - 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        
    }
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
        {
            return 60;
        }
        default:
            return 44;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == _dataSource.count - 1) {
        [self pushViewController:[MyQRCodeViewController new]];
    }else if (indexPath.row == 0) {
        CameraActionSheet *actionSheet = [[CameraActionSheet alloc] initWithActionTitle:nil TextViews:nil CancelTitle:@"取消" withDelegate:self otherButtonTitles:@"从相册选择", @"拍一张", nil];
        [actionSheet show];

    }
}
#pragma mark -
#pragma mark --CameraActionSheetDelegate--
- (void)cameraActionSheet:(CameraActionSheet *)sender didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 2) {
        return;
    }
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if (buttonIndex == 0){
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else if (buttonIndex == 1) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        } else {
            //                [self showText:@"无法打开相机"];
        }
    }
    [self presentModalController:picker animated:YES];
}
#pragma mark --UIImagePickerControllerDelegate--
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        img = [UIImage rotateImage:img];
        VPImageCropperViewController *imgCropperVC = [[VPImageCropperViewController alloc] initWithImage:img cropFrame:CGRectMake((self.view.frame.size.width - 240)/2, 100.0f, 240, 240) limitScaleRatio:1.0 title:@"上传素材"];
        [imgCropperVC setCompletionBlock:^(BOOL didFinished, UIImage *editedImage) {
            if (editedImage) {
                
            }
        }];
        [self pushViewController:imgCropperVC];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
